<?php
    require_once '../funciones.php';

	$page = $_REQUEST['page']; 

	// get how many rows we want to have into the grid - rowNum parameter in the grid 
	$limit = $_REQUEST['rows']; 

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel 
	$sidx = $_REQUEST['sidx']; 

	// sorting order - at first time sortorder 
	$sord = $_REQUEST['sord']; 

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;

        $numero = $_REQUEST['numero'];
        $campo = (strtoupper($_REQUEST['documento']) == 'PED')?'pedido':'cambio';

        $con = con_caweb();

        $sql = "select * from seriales where $campo = '$numero' and estado = 'A'";
        $result = mysql_query($sql,$con) or die ("Error SQL 1".mysql_error().$sql);
        $count = mysql_num_rows($result);

	// calculate the total pages for the query 
	if( $count > 0 ) { 
				  $total_pages = ceil($count/$limit); 
	} else { 
				  $total_pages = 0; 
	} 

	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 

	// the actual query for the grid data
        $sql = $sql." ORDER BY $sidx $sord LIMIT $start , $limit";
	$result = mysql_query($sql,$con) or die("Error SQL 2.".mysql_error().$sql);

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";

	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		echo "<row id='". $row[interno]."'>";
                echo "<cell>". $row[interno]."</cell>";
		echo "<cell>". $row[item]."</cell>";
		echo "<cell>". $row[estado]."</cell>";
		echo "<cell>". $row[pedido]."</cell>";
		echo "<cell>". $row[cambio]."</cell>";
                echo "<cell>". $row[sucursalcontrol]."</cell>";
                echo "<cell>". $row[bodega]."</cell>";
                echo "<cell>". $row[calidad]."</cell>";
		//echo "<cell>". utf8_encode(htmlspecialchars($row[observacioncartera]))."</cell>";
		echo "</row>";
	}
	echo "</rows>";
	 
?>
